package com.tonann.app.domain;

import cn.hutool.core.util.StrUtil;
import com.tonann.common.annotation.Excel;
import com.tonann.common.annotation.Log;
import com.tonann.common.core.domain.BaseEntity;
import com.tonann.common.core.domain.entity.SysDictData;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.data.annotation.Id;

import java.math.BigDecimal;
import java.util.List;

/**
 * 传感器管理对象 app_sensor
 *
 * @author tonann
 * @date 2022-10-17
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Getter
@Setter
public class AppSensor extends BaseEntity
{
    private static final long serialVersionUID = 1L;

    /**
     * ID
     */
    @Id
    private Long sensorId;

    /**
     * 所属设备
     */
//    @Excel(name = "所属设备")
    private Long deviceId;


    @Excel(name = "设备名称")
    private String deviceName;

    /**
     * MQTT的订阅主题
     */
    @Excel(name = "MQTT的订阅主题")
    private String mqttTopic;

    /**
     * MQTT的通道号
     */
    @Excel(name = "MQTT的通道号")
    private String mqttDeviceChannel;

    /**
     * MQTT的设备名称
     */
    @Excel(name = "MQTT的设备名称")
    private String mqttDeviceName;

    /**
     * MQTT的标记
     */
    @Excel(name = "MQTT的标记")
    private String mqttDeviceTag;

    /**
     * MQTT写入主题
     */
    @Excel(name = "MQTT写入主题")
    private String mqttWriteTopic;

    /**
     * 告警规则
     */
//    @Excel(name = "告警规则Id")
    private Long faultRuleId;

    @Excel(name = "告警规则")
    private String ruleName;

    @Excel(name = "告警下限")
    private String alarmBottom;

    @Excel(name = "告警上限")
    private String alarmTop;

    /**
     * 传感器编号
     */
    @Excel(name = "传感器编号")
    private String sensorCode;

    /**
     * 传感器名字
     */
    @Excel(name = "传感器名字")
    private String sensorName;

    /** 传感器类型 */
    @Excel(name = "传感器类型", dictType =  "app_sensor_type")
    private String sensorType;

    /** 传感器取值地址 */
    @Excel(name = "PLC取值地址")
    private String outputAddress;

    /** 传感器设值地址 */
    @Excel(name = "PLC设值地址")
    private String inputAddress;

    /** 传感器值 */
//    @Excel(name = "传感器值")
    private String value;

    /** 取值周期 */
//    @Excel(name = "取值周期")
    private Long intervalTime;

    /** 值放大倍数 */
//    @Excel(name = "放大倍数")
    private Long magnification;

    /** 计量单位 */
    @Excel(name = "计量单位" , dictType =  "app_sensor_unit")
    private String unit;

    /** 面板显示(是/否) */
    @Excel(name = "面板显示")
    private String isDisplay;

    @Excel(name = "专门用途")
    private String specialPurpose;

    /** 是否参与能耗计算 */
//    @Excel(name = "能耗计算")
    private String isEnergy;

    /** 是否是灯控 */
//    @Excel(name = "是否灯控")
    private String isLight;

    /** 是否是排风阀 */
//    @Excel(name = "是否排风")
    private String isExhaust;

    /** 是否是控制窗高度 */
//    @Excel(name = "是否窗高")
    private String isHeight;

    /** 是否是电源开关 */
//    @Excel(name = "是否电源")
    private String isOn;

    /** 是否静音 */
//    @Excel(name = "是否静音")
    private String isMute;

    /** 是否强排 */
//    @Excel(name = "是否强排")
    private String isForce;

    /** 是否新风 */
//    @Excel(name = "是否新风")
    private String isFresh;

    /** 是否是有无人 */
//    @Excel(name = "是否有无人")
    private String isBeing;


    private String workMode;
    /** 三维编号 */
//    @Excel(name = "三维编号")/
    private String tdCode;

    private String isAutoDoor;


    @Excel(name = "部门名称")
    private String deptName;

    @Excel(name = "位置名称")
    private String placeName;

    private Long projectId;

    private Long[] deviceIds;

    private AppDevice device;

    private AppGateway gateway;

    private AppFaultRule faultRule;

    private Long[] deptIds;

    private Long[] placeIds;

    private String delFlag;

    private List<AppSensorMultiValued> sensorMultiValuedList;

    private List<SysDictData> sensorTypeDictList;

    private String valueDesc;

    private String typeDesc;

    //传感器中重复值的次数
    private int valueCount = 0;
    //传感器是否处于告警中
    private Boolean alarming = false;

    public String getValueDesc() {
        if (this.sensorMultiValuedList == null || this.sensorMultiValuedList.isEmpty()) {
            return null;
        }
        for (AppSensorMultiValued multiValued : sensorMultiValuedList){
            if (multiValued.getValue() != null && this.value != null && !StrUtil.isEmpty(this.value)){
                try {
                    BigDecimal multiValuedBigDecimal = new BigDecimal(multiValued.getValue());
                    BigDecimal valueBigDecimal = new BigDecimal(this.value);
                    if (multiValuedBigDecimal.compareTo(valueBigDecimal) == 0){
                        return  multiValued.getMeaning();
                    }
                }catch (Exception e){
                    System.out.println(e.getMessage() + ">>>>value = " + this.value);
                }
            }
        }
        return null;
    }

    public String getTypeDesc() {
        if (this.sensorTypeDictList == null || this.sensorTypeDictList.isEmpty()) {
            return null;
        }
        for (SysDictData dictData : sensorTypeDictList){
            if (dictData.getDictValue() != null && this.sensorType != null && this.sensorType.equals(dictData.getDictValue())){
                return  dictData.getDictLabel();
            }
        }
        return null;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
                .append("sensorId", getSensorId())
                .append("deviceId", getDeviceId())
                .append("mqttTopic", getMqttTopic())
                .append("mqttDeviceChannel", getMqttDeviceChannel())
                .append("mqttDeviceName", getMqttDeviceName())
                .append("mqttDeviceTag", getMqttDeviceTag())
            .append("mqttWriteTopic", getMqttWriteTopic())
            .append("faultRuleId", getFaultRuleId())
            .append("sensorCode", getSensorCode())
            .append("sensorName", getSensorName())
            .append("sensorType", getSensorType())
            .append("outputAddress", getOutputAddress())
            .append("inputAddress", getInputAddress())
            .append("value", getValue())
            .append("intervalTime", getIntervalTime())
            .append("magnification", getMagnification())
            .append("unit", getUnit())
            .append("isDisplay", getIsDisplay())
            .append("isEnergy", getIsEnergy())
            .append("isLight", getIsLight())
            .append("isExhaust", getIsExhaust())
            .append("isHeight", getIsHeight())
            .append("isOn", getIsOn())
            .append("isMute", getIsMute())
            .append("isForce", getIsForce())
            .append("isFresh", getIsFresh())
            .append("isBeing", getIsBeing())
            .append("tdCode", getTdCode())
            .append("createBy", getCreateBy())
            .append("updateBy", getUpdateBy())
            .append("createTime", getCreateTime())
            .append("updateTime", getUpdateTime())
            .toString();
    }
}
